<template>
    <div class="app-container">
      <!--工具栏-->
      <Search :crud="crud">
        <!-- <el-form-item label="商品编码">
          <el-input v-model="query.orderNum" placeholder="请输入商品编码" />
        </el-form-item> -->
        <el-form-item label="商品名称">
            <el-input v-model="query.platformGoodsName" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="query.enabled" placeholder="请选择">
            <el-option
              v-for="item in dict.COMMON_STATUS_DICT"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="商品状态">
            <el-input v-model="query.user" placeholder="请输入商品状态"></el-input>
        </el-form-item> -->
      </Search>
      
      <el-card>
        <div class="head-container">
            <crudOperation :permission="permission">
                <template slot="right">
                    <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="handle(null,'new')">新建</el-button>
                    <el-button class="filter-item" size="mini" type="primary" icon="el-icon-delete" @click="handle(null,'batchDelete')">批量删除</el-button>
                </template>
            </crudOperation>
        </div>
        <!--表格渲染-->
        <el-table ref="table" border v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
            <el-table-column type="selection" width="55" align="center" fixed="left"/>
            <el-table-column label="序号" type="index" width="50" align="center"/>
            <el-table-column prop="url" label="商品图片" align="center" width="120">
              <template slot-scope="scope" >
                <template v-if="scope.row.tbPlatformGoodsFileList != undefined && scope.row.tbPlatformGoodsFileList.length>0">
                  <el-badge :value="scope.row.tbPlatformGoodsFileList.length" class="item">
                    <el-image
                  style="width: 60px; height: 60px"
                  :src="scope.row.tbPlatformGoodsFileList[0].fileUrl"
                  fit="fill" @click="handle(scope.row,'checkImg')"></el-image>
                  </el-badge>
                </template>
              </template>
            </el-table-column>
            <el-table-column prop="platformGoodsName" label="商品名称" align="center" show-overflow-tooltip/>
            <!-- <el-table-column prop="code" label="商品编码" align="center" width="250" /> -->
            <el-table-column prop="platformPrice" label="商品价格" align="center"/>
            <el-table-column prop="finalPrice" label="券后价格" align="center"/>
            <!-- <el-table-column prop="quantitySold" label="售出数量" align="center"/> -->
            <!-- <el-table-column prop="returnQuantity" label="退货数量" align="center"/> -->
            <el-table-column prop="createdName" label="创建人" align="center"/>
            <el-table-column prop="updatedAt" label="创建日期" width="200" align="center"/>
            <el-table-column prop="status" label="商品状态" align="center" fixed="right">
              <template slot-scope="scope" >
                  <el-switch v-model="scope.row.enabled" active-value="1" inactive-value="0" @change="changeEnabled(scope.row, scope.row.enabled)"></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="200"  fixed="right">
              <template slot-scope="scope" >
                  <el-button type="danger" size="mini" @click="handle(scope.row,'delete')">删除</el-button>
                  <el-button type="primary" size="mini" @click="handle(scope.row,'update')">修改</el-button>
              </template>
            </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination />
      </el-card>
      <ProductDialog ref="productDialog" @submitSuccess="submitSuccess"></ProductDialog>
    </div>
  </template>
  
  <script>
  import crudJob from '@/api/system/job'
  import CRUD, { presenter, header, crud } from '@crud/crud'
  import crudOperation from '@crud/CRUD.operation'
  import pagination from '@crud/Pagination'
  import udOperation from '@crud/UD.operation'
  import Search from '@/components/Search'
  import ProductDialog from './productDialog.vue'
  import product from '@/api/product/product'
  export default {
    name: 'ProductManage',
    components: { crudOperation, pagination, udOperation, Search, ProductDialog },
    cruds() {
      return CRUD({
        title: '商品管理',
        url: 'goods/platform/getProductionList',
        requestMethod: 'post',
        sort: ['jobSort,asc', 'id,desc'],
        crudMethod: { ...crudJob }
      })
    },
    // mixins: [presenter()],
    mixins: [presenter(), header(), crud()],
    // 数据字典
    dicts: ['COMMON_STATUS_DICT'],
    data() {
      return {
        permission: {
          // add: ['admin', 'job:add'],
          // edit: ['admin', 'job:edit'],
          // del: ['admin', 'job:del']
        },
        dialogVisible: false
      }
    },
    methods: {
      // 改变状态
      changeEnabled(data, val) {
        product.goodsPlatformUpdateStatus({id:data.id,enabled:val}).then(() => {
          this.$notify({ message: '操作成功', type: 'success'});
          this.crud.refresh();
        }).catch(err => {
          data.enabled = !data.enabled
          if (data.enabled=='1') {
            data.enabled = '0';
          }else{
            data.enabled = '1';
          }
        })
      },
      // 删除与修改事件
      handle(row,type){
        if (type == 'delete') {//删除
          this.deleteHandle([row.id]);
        }else if (type == 'update') {// 修改
          let forms = JSON.parse(JSON.stringify(row));
          for (let index = 0; index < forms.tbPlatformGoodsFileList.length; index++) {
            const element = forms.tbPlatformGoodsFileList[index];
            element.name = element.fileName;
            element.url = element.fileUrl;
          }
          this.$refs.productDialog.form = forms;
          this.$refs.productDialog.isEdit = true;
          this.$refs.productDialog.dialogVisible = true;
        }else if (type == 'new') {// 新增
          this.$refs.productDialog.form = {
              platformGoodsName:'',//商品名称
              platformPrice: 0, // 单价
              finalPrice:0, // 券后价格
              enabled:0, // 是否启用
              productDescription:'', // 商品描述
              tbPlatformGoodsFileList:[]
          }
          this.$refs.productDialog.isEdit = false;
          this.$refs.productDialog.dialogVisible = true;
        }else if (type == 'batchDelete') {// 批量删除
          if (this.crud.selections.length==0) {
            this.$notify({
              title: '请选择需要删除的数据', type: 'warning'
            });
            return;
          }
          const idList = [];
          for (let index = 0; index < this.crud.selections.length; index++) {
            const element = this.crud.selections[index];
            idList.push(element.id);
          }
          console.log(JSON.stringify(idList));
          this.deleteHandle(idList);
          
        }else if (type == 'checkImg') {//查看图片
          let urlList = [];
          for (let index = 0; index < row.tbPlatformGoodsFileList.length; index++) {
            const element = row.tbPlatformGoodsFileList[index];
            urlList.push(element.fileUrl);
            
          }
          this.$viewerApi({ images: urlList })
        } else {
          
        }
      },

      // 删除
      deleteHandle(params){
        this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          product.goodsPlatformDelete(params).then((res) => {
            this.$notify({ message: '操作成功', type: 'success'});
            this.crud.refresh()
          }).catch(() => {})
        })
      },
      // 新建商品成功
      submitSuccess(){
        this.crud.refresh();
      }
    }
  }
  </script>
  
  <style rel="stylesheet/scss" lang="scss" scoped>
   ::v-deep .el-input-number .el-input__inner {
      text-align: left;
    }

    .item {
      margin-top: 10px;
      margin-right: 10px;
    }
  </style>
  